<script>
import {createModelForVue, createProps} from '@/utils';
import {createFormDefinition} from '@/components/hhd/design/FormDesigner/types';
import FormEngineUtils from '@/components/hhd/design/FormEngine/FormEngineUtils';
import FormEngineRender from '@/components/hhd/design/FormEngine/FormEngineRender';

export default {
  name: 'FormEngine',
  model: createModelForVue('formModel'),
  props: {
    formDefinition: createProps(Object, createFormDefinition),
    view: createProps(String),
    formModel: createProps(Object, () => undefined),
    action: createProps(String)
  },
  data() {
    return {
      context: undefined
    }
  },
  render() {
    return this.checkPropsValues() ? this.renderForm() : undefined;
  },
  methods: {
    renderForm() {
      let context = this.context = FormEngineUtils.createContext(this);
      return context.check() ? FormEngineRender.render(context) : undefined;
    },
    checkPropsValues() {
      return !!this.formDefinition && !!this.view && !!this.action;
    },
    resetAction() {
      this.context && this.context.resetAction();
    },
  }
};
</script>